﻿<%@ WebHandler Language="C#" Class="sendmail" %>

using System;
using System.Web;
using TSqlServer;
using System.Net.Mail;

public class sendmail : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        
        string puid = context.Request["puid"].ToString();
        string email = context.Request["email"].ToString();
        string sSql = "";
        
        context.Response.ContentType = "text/plain";

        if (mySendMail(puid, email))
        {
            sSql = "insert into t_sendmail_log (pieceid,email,iflag) values('" + puid + "','" + email + "',0)";
            TSqlDB.ExecuteNonQuery(sSql);
            
            context.Response.Write("ok");
        }
        else
        {
            sSql = "insert into t_sendmail_log (pieceid,email,iflag) values('" + puid + "','" + email + "',1)";
            TSqlDB.ExecuteNonQuery(sSql);

            context.Response.Write("failed");
        }
        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    private bool mySendMail(string puid,string email)
    {
        bool result = true;
        
        //获得内容
        TCMSPiece oPiece = new TCMSPiece(puid);
        oPiece.Select();

        //准备邮件
        MailAddress fromemail = new MailAddress("admin@cnap.com.cn", "中国汽车在线");
        MailAddress toemail = new MailAddress(email);
        MailMessage oMail = new MailMessage(fromemail, toemail);
        oMail.Subject = oPiece.PieceName;
        oMail.Priority = MailPriority.Normal;
        oMail.IsBodyHtml = true;
        oMail.Body = oPiece.PieceContent;

        try
        {
            //发送邮件
            SmtpClient oSmtp = new SmtpClient("smtp.cnap.com.cn");
            oSmtp.Credentials = new System.Net.NetworkCredential("admin@cnap.com.cn", "mala990707");
            oSmtp.Send(oMail);
        }
        catch (Exception e)
        {
            result = false;
        }

        return result;
    }

}